﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using OpenGado.DATA;
using OpenGado.DATA.Entities;

namespace OpenGado.Win
{
    public partial class frmProjectManagement : Form
    {
        public frmProjectManagement()
        {
            InitializeComponent();
            GetProjectList();
        }

        private void GetProjectList()
        {
            dgvProjectList.AutoGenerateColumns = false;

            IList<Project> projectList = new List<Project>();
            projectList = ProjectDataControl.Instance.GetAllProjects();

            
            dgvProjectList.DataSource = projectList;
            dgvProjectList.Columns[0].DataPropertyName = "Projectid";
            dgvProjectList.Columns[1].DataPropertyName = "Name";
            dgvProjectList.Columns[2].DataPropertyName = "Coordinator";
            dgvProjectList.Columns[3].DataPropertyName = "Code";
            dgvProjectList.Columns[4].DataPropertyName = "StartingDate";
            dgvProjectList.Columns[5].DataPropertyName = "EndDate";
            

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            frmProjects frmpj = new frmProjects();
            frmpj.UpdateProjectList += new frmProjects.UpdateProjectListHandler(GetProjectList);
            frmpj.Show();
        }

        private List<int> GetSelectedProjects()
        {
            List<int> selectedProjects = new List<int>();
            int projectId ; 
            foreach (DataGridViewRow rw in dgvProjectList.SelectedRows)
            {
                int.TryParse(rw.Cells[0].Value.ToString(), out projectId);

                if (!selectedProjects.Contains(projectId))
                {
                    selectedProjects.Add(projectId);
                }

            }
           
            return selectedProjects;
        }

        private void dgvProjectList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            int projectId;

            int.TryParse(dgvProjectList.CurrentRow.Cells[0].Value.ToString(), out projectId);

            frmProjects frmPj = new frmProjects(projectId);
            frmPj.UpdateProjectList += new frmProjects.UpdateProjectListHandler(GetProjectList);
            frmPj.Show();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DialogResult res;

            res = MessageBox.Show("Are you sure you want to delete the selected item(s)", "Inventory", MessageBoxButtons.YesNo);

            if (res == DialogResult.Yes)
            {
                // Get selction 
                foreach (int i in GetSelectedProjects())
                {
                    Project p = new Project();
                    p.Projectid = i;

                    ProjectDataControl.Instance.Delete(p);
                }
                
                GetProjectList();
            }
        }
    }
}
